package com.acwing.chapter3;

import java.io.*;

/**
 * @author anan
 * @create 2021-08-25 17:31
 */
public class Trie835 {
    private static int N = 100010;
    private static int[][] son = new int[N][26];
    private static int[] cnt = new int[N];
    private static int idx = 0;

    public static void insert(char[] str) {
        int p = 0;
        int u = 0;
        for (int i = 0; i < str.length; i++) {
            u = str[i] - 97;
            if (son[p][u] == 0){
                son[p][u] = ++idx;
            }
            p = son[p][u];
        }
        cnt[p]++;
    }

    public static int query(char[] str){
        int p = 0;
        int u = 0;
        for (int i = 0; i < str.length; i++) {
            u = str[i] - 97;
            if (son[p][u] == 0) return 0;
            p = son[p][u];
        }
        return cnt[p];
    }

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
        int N = 0;
        String[] str1 = reader.readLine().split(" ");
        N = Integer.parseInt(str1[0]);
        while (N-- > 0){
            String[] str2 = reader.readLine().split(" ");

            char[] chars = str2[1].toCharArray();
            if (str2[0].equals("I")) insert(chars);
            else if("Q".equals(str2[0])) {
                writer.write(query(chars) + "\n");
                //System.out.println(query(chars));
            }
        }
        writer.flush();
        writer.close();
        reader.close();

    }

}
